package com.atguigu.flink.sql;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * Created by Smexy on 2023/3/5
 */
public class Demo12_TVFWindowGroupingSets
{
    public static void main(String[] args) {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        StreamTableEnvironment tableEnvironment = StreamTableEnvironment.create(env);

        String createTableSql = " create table t1 ( id string, ts bigint  , vc int , " +
            "                   et  as TO_TIMESTAMP_LTZ(ts,3)    ,   " +
            "                   pt  as  PROCTIME() ," +
            "                   WATERMARK FOR et AS et - INTERVAL '1' SECOND   )" +
            " with ( " +
            " 'connector' = 'filesystem' ,   " +
            " 'path' =  'data/cep.txt' ,   " +
            "  'format' = 'csv' " +
            "      )                 ";

        tableEnvironment.executeSql(createTableSql);

        String sql1 = " select  window_start, window_end, id,sum(vc) sumVc " +
            "  from TABLE(" +
            "    TUMBLE(TABLE t1, DESCRIPTOR(et), INTERVAL '5' SECONDS)) " +
            "  GROUP BY window_start, window_end ," +
            //"  grouping sets ((id) , ()) "
            //"  cube(id) "
            "  rollup(id) "
            ;


        tableEnvironment.sqlQuery(sql1)
                        .execute()
                        .print();

    }
}
